const express = require("express");
const router = express.Router();
const models = require("../models");
const Op = models.Sequelize.Op; // 模糊查询的时候需要用到

// 分页列活动
router.get("/list", async function (req, res, next) {
  const where = {};
  const currentPage = parseInt(req.query.currentPage) || 1; // 分页参数
  const pageSize = parseInt(req.query.pageSize) || 10; // 分页参数
  const form = req.query.form;
  const type = req.query.type;
  const status = req.query.status;
  const society = req.query.society;
  const name = req.query.name;
  const person = req.query.person;
  if (form) {
    where.form = {
      [Op.like]: form,
    };
  }
  if (type) {
    where.type = {
      [Op.like]: type,
    };
  }
  if (status) {
    where.status = {
      [Op.like]: status,
    };
  }
  if (name) {
    where.name = {
      [Op.like]: "%" + name + "%",
    };
  }
  if (society) {
    where.society = {
      [Op.like]: "%" + society + "%",
    };
  }
  if (person) {
    where.person = {
      [Op.like]: "%" + ("-" + person + "-") + "%",
    };
  }
  const result = await models.Activity.findAndCountAll({
    order: [["id", "desc"]], // 根据某个字段进行排序， desc-降序 asc-升序
    where: where,
    offset: (currentPage - 1) * pageSize,
    limit: pageSize,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      total: result.count,
      currentPage: currentPage,
      pageSize: pageSize,
      data: result.rows,
    },
  });
});

// 分页列参与活动的人员
router.get("/personList", async function (req, res, next) {
  const where = {};
  const currentPage = parseInt(req.query.currentPage) || 1; // 分页参数
  const pageSize = parseInt(req.query.pageSize) || 10; // 分页参数
  const person = req.query.person.split("-");
  where.id = {
    [Op.in]: person,
  };
  const result = await models.User.findAndCountAll({
    order: [["id", "desc"]], // 根据某个字段进行排序， desc-降序 asc-升序
    where: where,
    offset: (currentPage - 1) * pageSize,
    limit: pageSize,
  });
  res.json({
    code: 200,
    msg: "操作成功",
    data: {
      total: result.count,
      currentPage: currentPage,
      pageSize: pageSize,
      data: result.rows,
    },
  });
});

// 新增活动
router.post("/", async function (req, res, next) {
  await models.Activity.create({
    name: req.body.name,
    planNumber: req.body.planNumber,
    nowNumber: 1,
    form: req.body.form,
    type: req.body.type,
    applicationStartTime: req.body.date1[0],
    applicationEndTime: req.body.date1[1],
    formalStartTime: req.body.date2[0],
    formalEndTime: req.body.date2[1],
    society: req.body.society,
    status: 0,
    introduce: req.body.introduce,
    examine: req.body.examine,
    imgurl: req.body.imgurl,
    planBook: req.body.planBook,
    person: "-" + req.body.person + "-",
    address: req.body.address,
    score: req.body.score,
  });
  res.json({
    code: 200,
    msg: "操作成功",
  });
});

// 修改活动
router.put("/", async function (req, res, next) {
  var data = await models.Activity.findByPk(req.body.id);
  data.update(req.body);
  res.json({
    code: 200,
    msg: "操作成功",
  });
});



module.exports = router;
